$(document).ready(function(){
    const userAccount = $.cookie("userAccount");
    /**
     * 判断是否登录
     */
    if (typeof userAccount == "undefined" || userAccount === "") {
        location.href = "/index";
    }

    /**
     * 获取用户信息
     */
    $.ajax({
        type: "get",
        url: "/api/user/u/getUserByAccount",
        dataType: "text",
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                const user = JSON.parse(data.data);
                $("#user input[name='userAccount']").val(user.userAccount);
                $("#user input[name='userName']").val(user.userName);
                $("#user input[name='userSex']").val(user.userSex);
                $("#user input[name='userAge']").val(user.userAge);
                $("#userName").text(user.userName);
            }else {
                alert("账户名或者密码错误!");
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    })

    /**
     * 修改用户信息
     */
    $("#user_btn").click(function () {
        const userAccount = $("#user input[name='userAccount']").val();
        const userName = $("#user input[name='userName']").val();
        const userSex = $("#user input[name='userSex']").val();
        const userAge = $("#user input[name='userAge']").val();
        const userPassword = $("#user input[name='userPassword']").val();
        const userNewPassword = $("#user input[name='userNewPassword']").val();
        const userComfirmPassword = $("#user input[name='userComfirmPassword']").val();
        var flag = true;
        if (userPassword === "") {
            $("#user input[name='userPassword']").prev().css("color", "red");
            flag = false;
        }else {
            $("#user input[name='userPassword']").prev().css("color", "");
        }
        if (userNewPassword !== userComfirmPassword) {
            $("#user input[name='userComfirmPassword']").prev().css("color", "red");
            flag = false;
        }else {
            $("#user input[name='userComfirmPassword']").prev().css("color", "");
        }
        if (flag) {
            $.ajax({
                type: "post",
                url: "/api/user/u/modifyUser",
                dataType: "text",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify({
                    "userAccount": userAccount,
                    "userPassword": userNewPassword!=="" ? userNewPassword + "_" + userPassword: userPassword,
                    "userName": userName,
                    "userSex": userSex,
                    "userAge": userAge
                }),
                success: data => {
                    data = eval('('+ data +')');
                    if (data.success) {
                        $.cookie("userAccount", userAccount);
                        location.reload();
                    }else {
                        alert(data.msg);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    const msg = xhr.responseText;
                    console.log(msg);
                }
            })
        }
    })

    /**
     * 登出
     */
    $("a[name='signOut']").click(function () {
        $.removeCookie('userAccount');
    })

    /**
     * 获取用户的收货地址
     */
    $.ajax({
        type: "get",
        url: "/api/address/u/getAddressByUser",
        dataType: "text",
        success: data => {
            data = eval('('+ data +')');
            if (data.success) {
                const addresses = JSON.parse(data.data);
                if (addresses.length > 0) {
                    addresses.forEach((address, index) => {
                        $("#addresslist").append("<tr><td><address>" + address.address + "</address></td>" +
                            "<td><address>" + address.phone + "</address></td>" +
                            "<td><address><button type='button' name='delAddress'>删除</button><address></td></tr>")
                    })
                }
            }else {

            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    })

    /**
     * 删除地址
     */
    $("body").on("click", "button[name='delAddress']",function () {
        const phone = this.parentNode.parentNode.previousSibling.innerText;
        const address = this.parentNode.parentNode.previousSibling.previousSibling.innerText;
        var formData = new FormData();
        formData.append('address', address);
        $.ajax({
            type: "post",
            url: "/api/address/u/deleteAddress",
            dataType: "text",
            processData: false,
            contentType: false,
            data: formData,
            success: data => {
                data = eval('('+ data +')');
                if (data.success) {
                    this.parentNode.parentNode.parentNode.remove();
                }else {
                    alert(data.msg);
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                const msg = xhr.responseText;
                console.log(msg);
            }
        })
    })

    /**
     * 添加地址
     */
    $("#address_btn").click(function () {
        const address = $("#address input[name='address']").val();
        const phone = $("#address input[name='phone']").val();

        if (address === "") {
            $("#address input[name='address']").attr("placeholder","地址不能为空");
        }
        if (phone === "") {
            $("#address input[name='phone']").attr("placeholder","手机号不能为空");
        }else {
            $.ajax({
                type: "post",
                url: "/api/address/u/addUserAddress",
                dataType: "text",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify({
                    "address": address,
                    "phone": phone
                }),
                success: data => {
                    data = JSON.parse(data);
                    if (data.success) {
                        $("#addresslist").append("<tr><td><address>" + address + "</address></td>" +
                            "<td><address>" + phone + "</address></td>" +
                            "<td><address><button type='button' name='delAddress'>删除</button><address></td></tr>")
                    }else {
                        alert(data.msg);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    const msg = xhr.responseText;
                    console.log(msg);
                }
            })
        }
    })
});